﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YJKColumnModify.ConnectColumnBream
{
	internal class NewBream
	{
		public NewBream(ConnectColumnBreamInfo inConnectBream)
		{
			this.m_commandData = inConnectBream.ConnectBreamInfo.ThisExternalCommandData;
			this.m_uiDoc = this.m_commandData.Application.ActiveUIDocument;
			this.m_doc = this.m_uiDoc.Document;
		}

		public void CreateBream(ConnectColumnBreamInfo inConnectBream)
		{
			Transaction transaction = new Transaction(this.m_doc, "CreateBream");
			try
			{
				transaction.Start();
				LocationCurve locationCurve = inConnectBream.ConnectBreamInfo.Bream.Location as LocationCurve;
				XYZ xyz = null;
				XYZ xyz2 = null;
				if (inConnectBream.SerialNumber == 0)
				{
					xyz = inConnectBream.ConnectPoint;
					xyz2 = locationCurve.Curve.GetEndPoint(1);
				}
				if (inConnectBream.SerialNumber == 1)
				{
					xyz = locationCurve.Curve.GetEndPoint(0);
					xyz2 = inConnectBream.ConnectPoint;
				}
				Line curve = Line.CreateBound(xyz, xyz2);
				locationCurve.Curve = curve;
				for (int i = 0; i < inConnectBream.ArrayDetailCurve.Length; i++)
				{
					this.m_doc.Delete(inConnectBream.ArrayDetailCurve[i].Id);
				}
				transaction.Commit();
				transaction.Start();
				locationCurve = (inConnectBream.ConnectBreamInfo.Bream.Location as LocationCurve);
				locationCurve.Curve = curve;
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public void CreateBream(List<ConnectColumnBreamInfo> lstConnectBream)
		{
			Transaction transaction = new Transaction(this.m_doc, "CreateBream");
			try
			{
				transaction.Start();
				foreach (ConnectColumnBreamInfo connectColumnBreamInfo in lstConnectBream)
				{
					LocationCurve locationCurve = connectColumnBreamInfo.ConnectBreamInfo.Bream.Location as LocationCurve;
					XYZ xyz = null;
					XYZ xyz2 = null;
					if (connectColumnBreamInfo.SerialNumber == 0)
					{
						xyz = connectColumnBreamInfo.ConnectPoint;
						xyz2 = locationCurve.Curve.GetEndPoint(1);
					}
					if (connectColumnBreamInfo.SerialNumber == 1)
					{
						xyz = locationCurve.Curve.GetEndPoint(0);
						xyz2 = connectColumnBreamInfo.ConnectPoint;
					}
					Line curve = Line.CreateBound(xyz, xyz2);
					locationCurve.Curve = curve;
					for (int i = 0; i < connectColumnBreamInfo.ArrayDetailCurve.Length; i++)
					{
						this.m_doc.Delete(connectColumnBreamInfo.ArrayDetailCurve[i].Id);
					}
				}
				transaction.Commit();
				transaction.Start();
				foreach (ConnectColumnBreamInfo connectColumnBreamInfo2 in lstConnectBream)
				{
					LocationCurve locationCurve2 = connectColumnBreamInfo2.ConnectBreamInfo.Bream.Location as LocationCurve;
					XYZ xyz3 = null;
					XYZ xyz4 = null;
					if (connectColumnBreamInfo2.SerialNumber == 0)
					{
						xyz3 = connectColumnBreamInfo2.ConnectPoint;
						xyz4 = locationCurve2.Curve.GetEndPoint(1);
					}
					if (connectColumnBreamInfo2.SerialNumber == 1)
					{
						xyz3 = locationCurve2.Curve.GetEndPoint(0);
						xyz4 = connectColumnBreamInfo2.ConnectPoint;
					}
					Line curve2 = Line.CreateBound(xyz3, xyz4);
					locationCurve2.Curve = curve2;
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public void DeleteTempCurve(List<ConnectColumnBreamInfo> lstConnectBream)
		{
			Transaction transaction = new Transaction(this.m_doc, "DeleteTempCurve");
			try
			{
				transaction.Start();
				foreach (ConnectColumnBreamInfo connectColumnBreamInfo in lstConnectBream)
				{
					for (int i = 0; i < connectColumnBreamInfo.ArrayDetailCurve.Length; i++)
					{
						this.m_doc.Delete(connectColumnBreamInfo.ArrayDetailCurve[i].Id);
					}
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public void DeleteTempCurve(ConnectColumnBreamInfo connectBream)
		{
			Transaction transaction = new Transaction(this.m_doc, "DeleteTempCurve");
			try
			{
				transaction.Start();
				for (int i = 0; i < connectBream.ArrayDetailCurve.Length; i++)
				{
					this.m_doc.Delete(connectBream.ArrayDetailCurve[i].Id);
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		private ExternalCommandData m_commandData;

		private UIDocument m_uiDoc;

		private Document m_doc;
	}
}
